package com.litesuits.sdk.listener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import com.litesuits.android.log.Log;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.request.AbstractRequest;
import com.litesuits.http.response.Response;
import com.litesuits.sdk.LiteSdk;
import com.litesuits.sdk.R;

public abstract class AbsProgressHttpListener<T> extends AbsActivityHttpListener<T> {
    private static final String TAG = AbsProgressHttpListener.class.getSimpleName();
    ProgressDialog loadingProgressDialog;
    boolean isProgressCancelable = true;
    boolean isProgressDismissed = false;

    public AbsProgressHttpListener(Activity activity) {
        super(activity);
    }

    public AbsProgressHttpListener(Activity linkedActivity, boolean isProgressCancelable) {
        super(linkedActivity);
        this.isProgressCancelable = isProgressCancelable;
    }

    @Override
    public boolean disableListener() {
        return super.disableListener() || isProgressDismissed;
    }

    @Override
    public void onStart(AbstractRequest<T> request) {
        super.onStart(request);
        if (!isActivityDestroy()) {
            if (loadingProgressDialog == null) {

                loadingProgressDialog = new ProgressDialog(linkedActivity);
                //loadingProgressDialog.setTitle(R.string.tip_forgetpsw);
                loadingProgressDialog.setMessage(linkedActivity.getString(getMessage()));
                loadingProgressDialog.setIndeterminate(true);
                loadingProgressDialog.setCancelable(isProgressCancelable);
                loadingProgressDialog.setCanceledOnTouchOutside(false);
                loadingProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        isProgressDismissed = true;
                    }
                });
                loadingProgressDialog.show();
            } else if (!loadingProgressDialog.isShowing()) {
                loadingProgressDialog.setCancelable(isProgressCancelable);
                loadingProgressDialog.show();
            }
            isProgressDismissed = false;
        }
    }

    public int getTitle() {
        return R.string.tips_label;
    }

    public int getMessage() {
        return R.string.tips_waiting;
    }

    @Override
    public void onSuccess(T t, Response<T> response) {
        super.onSuccess(t, response);
        dismissWindow();
    }

    @Override
    public void onFailure(HttpException e, Response<T> response) {
        dismissWindow();
        LiteSdk.getInstance().getHttpExceptionHandler().via(linkedActivity).handleException(e);
    }

    private void dismissWindow() {
        if (!isActivityDestroy()) {
            if (loadingProgressDialog != null && loadingProgressDialog.isShowing()) {
                loadingProgressDialog.dismiss();
            }

        } else {
            Log.e(TAG, "android.view.WindowLeaked: " + linkedActivity);
        }
    }


}